import xflSelect from '@/components/xfl-select/xfl-select.vue'

const app = getApp()

export default {
	components:{
		xflSelect,
	},
	
	data(){
		return {
			court:'',
			building: '',
			unit: '',
			room:'',
			// 地区信息
			region:[],
			// 房间信息
			opts:{
				communities:[],				// 小区列表
				builds:[],						// 楼栋列表
				units:[],							// 单元列表
				rooms:[],							// 房间列表
			},
			communities:[],
			builds:[],
			units:[],
			rooms:[],
			
			post:{
				roomId:'',
			}
		}
	},
	
	methods:{
		// 切换地区
		changeRegion(evt){
			const {detail} = evt
			const {value, code, postcode} = detail
			this.region = value
			this.code = code
			console.log(detail)
			this.getCommunities();
		},
		// 切换小区
		selCommunity(evt){
			const {index} = evt
			const id = this.communities[index].id
			// 记录下小区ID
			this.court = id
			uni.request({
				header:{
					Authorization: 'Bearer ' + app.globalData.token,
				},
				url: app.globalData.curl + 'building/listByCourtId',
				method: 'GET',
				data: {
					courtId: id, 
				},
				success: res => {
					const {data} = res
					if(data.code==200){
						this.opts.builds=[]
						this.builds = data.data
						this.opts.builds.push(...this.builds.map((build)=>build.name))
					}else if(data.code==500){
						uni.showToast({
							icon:'none',
							title: data.mesasge,
						})
					}
				},
				fail: (err) => {console.error(err)},
				complete: () => {}
			});
		},
		// 切换楼宇
		selBuilding(evt){
			const {index} = evt
			const build = this.builds[index]
			// 记录下楼宇id（楼号）
			this.building = build.id
			const units = []
			this.opts.units=[]
			for (let i = 1; i <= this.builds[index].unit; i++) {
				units.push(`${i}单元`)
			}
			this.opts.units.push(...units)
		},
		// 切换单元
		selUnit(evt){
			const {index} = evt
			// 记录下单元号
			this.unit = index+1
			uni.request({
				header:{
					Authorization: 'Bearer ' + app.globalData.token,
				},
				url: app.globalData.curl + 'room/listByBuildingId',
				method: 'GET',
				data: {
					buildingId: this.building,
					unit: this.unit,
				},
				success: res => {
					const {data} = res
					if(data.code==200){
						this.rooms=[]
						this.opts.rooms = []
						if(data.data){
							this.rooms.push(...data.data)
							this.opts.rooms.push(...data.data.map((room)=>room.no))
						}
					}else if(data.code==500){
						uni.showToast({
							icon:'none',
							title: data.message,
						})
					}
				},
				fail: (err) => {console.error(err)},
				complete: () => {}
			});
		},
		// 切换房号
		selRoom(evt){
			const {index} = evt
			const room = this.rooms[index].id
			this.post.roomId = room
		},
		// 获取小区
		getCommunities(){
			uni.request({
				header:{
					Authorization: 'Bearer ' + app.globalData.token,
				},
				url:  app.globalData.curl + 'court/listAll',
				method: 'GET',
				data: {
					areaId: this.code[2] + '000000',
				},
				success: res => {
					const {data} = res
					if(data.code==200){
						this.communities=data.data
						this.opts.communities=data.data.map((community)=>community.name)
					}else if(data.code==500){
						uni.showToast({
							title: data.message,
						})
					}
				},
				fail: (err) => {console.error(err)},
			});
		},
	}
}